/*
 *  ListView.h
 *  LID Library
 *
 *  Created by Alexander Trutchenko on 09/11/11.
 *  Copyright 2011 League of Independent Developers. All rights reserved.
 *
 */

#ifndef __CE_ListView_h__
#define __CE_ListView_h__


#include "cocos2dx_extentions/controls/CEScrollView.h"
#include "cocos2dx_extentions/controls/CEListItem.h"

#include<deque>

namespace cocosext {

typedef enum ECEListChangeAction
{
	ece_lca_add = 0,
	ece_lca_remove,
	ece_lca_insert_before,
	ece_lca_insert_after,
} ECEListChangeAction;

struct CEListChangeInfo
{
	CEListItems items;
	CEListItem* item;
	ECEListChangeAction action;

public:
	CEListChangeInfo(): item(NULL), action(ece_lca_add) {;}
	CEListChangeInfo(ECEListChangeAction action_, CEListItems items_, CEListItem* item_ = NULL): items(items_), item(item_), action(action_) {;}
};

/////////////////////////////////////////////////////
//
/** CEVerticalListView 
* 
*/
/////////////////////////////////////////////////////

class CEScrollMenu;

class CEVerticalListView : public CEScrollView
{
	typedef std::deque<CEListChangeInfo> CEChanges;

protected:
	CEScrollMenu* _menu;
	float _upDownMargin;
	CEListItems _items; //weak refs
	bool _listIsBlocked;
	CEChanges _changes;

protected:
	CEVerticalListView(bool horizontalScrolling,
					   bool verticalScrolling,
					   bool insideScrollbar,
					   bool hiddenScrollbar);

    virtual bool init(cocos2d::CCNode* background,
					  CEScrollBar* horizontalScrollBar,
					  CEScrollBar* verticalScrollBar);

public:
    virtual ~CEVerticalListView() {;}

	/* Static constructor */
    static CEVerticalListView* node(cocos2d::CCNode* background = NULL,
								    bool horizontalScrolling = true,
								    CEScrollBar* horizontalScrollBar = NULL,
								    bool verticalScrolling = true,
								    CEScrollBar* verticalScrollBar = NULL,
								    bool insideScrollbar = true,
								    bool hiddenScrollbar = true);

public:
	virtual void addListItem(CEListItem * item);
	virtual void insertListItemAfter(CEListItem* item, CEListItem* after);
	virtual void insertListItemBefore(CEListItem* item, CEListItem* before);
	virtual void removeListItem(CEListItem * item);

	virtual void addListItems(const CEListItems& items);
	virtual void insertListItemsAfter(const CEListItems& items, CEListItem* after);
	virtual void insertListItemsBefore(const CEListItems& items, CEListItem* before);
	virtual void removeListItems(const CEListItems& items);

	virtual void removeAllListItems();
	
	float upDownMargin() const { return _upDownMargin; }
	void setUpDownMargin(float upDownMargin);

	virtual void setTouchPriority(int newPriority); 
	void setRelativeListPosition(float pos); //0.f .. 1.f

//Cocos2d interface
	virtual void setContentSize(const cocos2d::CCSize& contentSize);

protected:
	virtual void doSetIsEnabled(bool isEnabled);
	virtual void onMoveScrollableArea();

	virtual void updateScrollableAreaSize();
	virtual void updateItemsPosition();
	virtual void updateItemsVisibleState();

	virtual void processListChanges();
	virtual void updateList();

}; //CEVerticalListView


}; // end namespace cocosext

#endif // __CE_ListView_h__
